/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */

/*
 * PathPanel.java
 *
 * Created on 26-May-2011, 12:11:52
 */
package acid.plugin.gui;

import acid.network.AcidPath;
import cytoscape.CyEdge;
import cytoscape.CyNode;
import cytoscape.Cytoscape;
import edu.asu.emit.qyan.alg.model.abstracts.BaseVertex;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author morgan
 */
public class PathPanel extends javax.swing.JPanel
{

    private Collection<AcidPath> paths;
    private int columns = 5;

    /** Creates new form PathPanel */
    public PathPanel(Collection<AcidPath> paths, boolean useExpressionData)
    {
        this.paths = paths;
        if (useExpressionData)
        {
            columns++;
        }

        initComponents();
        SelectionListener listener = new SelectionListener(jTable1);
        jTable1.getSelectionModel().addListSelectionListener(listener);
        jTable1.getColumnModel().getSelectionModel().addListSelectionListener(listener);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        Object[][] data = new Object[paths.size()][columns];
        int i=0;
        for (AcidPath p: paths)
        {
            data[i][0] = i+1;
            data[i][1] = p;
            data[i][2] = p.get_vertices().size()-1;
            data[i][3] = p.findPathWeight();
            data[i][4] = p.get_weight();
            if (columns>5)
            data[i][5] = p.getExpressionWeight();
            i++;
        }
        jTable1.setAutoCreateRowSorter(true);
        String[] colNames=null;
        if (columns>5)
        colNames = new String [] {"Rank", "Proteins", "Path length",
            "Path weight", "Confidence", "Expression"};
        else
        colNames = new String [] {"Rank", "Proteins", "Path length",
            "Path weight", "Confidence"};
        jTable1.setModel(new javax.swing.table.DefaultTableModel(data,colNames)
            {
                Class[] types = new Class [] {
                    java.lang.Short.class, java.lang.Object.class,
                    java.lang.Short.class, java.lang.Double.class,
                    java.lang.Double.class, java.lang.Double.class
                };
                boolean[] canEdit = new boolean [] {
                    false, false, false, false, false, false
                };

                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
            }
        );
        jScrollPane1.setViewportView(jTable1);

        add(jScrollPane1);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    class SelectionListener implements ListSelectionListener
    {

        JTable table;

        SelectionListener(JTable table)
        {
            this.table = table;
        }

        @Override
        public void valueChanged(ListSelectionEvent e)
        {


            if (e.getSource() == table.getSelectionModel() && table.getRowSelectionAllowed())
            {
                Cytoscape.getCurrentNetwork().unselectAllNodes();
                Cytoscape.getCurrentNetwork().unselectAllEdges();
                int last = table.convertRowIndexToModel(table.getSelectedRow());
                System.out.println("Row " + last);

                AcidPath path = (AcidPath) table.getModel().getValueAt(last, 1);
                selectNodes(path);
                selectEdges(path);
                Cytoscape.getCurrentNetworkView().redrawGraph(true, true);
            }
        }

        private void selectNodes(AcidPath path)
        {
            List<CyNode> nodes = new ArrayList<CyNode>();
            for (BaseVertex v : path.get_vertices())
            {
                nodes.add(Cytoscape.getCyNode(new Integer(v.get_id()).toString()));
            }

            Cytoscape.getCurrentNetwork().setSelectedNodeState(nodes, true);
        }

        private void selectEdges(AcidPath path)
        {
            List<CyEdge> edges = new ArrayList<CyEdge>();
            CyNode prevNode = null;
            for (BaseVertex v : path.get_vertices())
            {
                if (prevNode != null)
                {
                    CyNode currNode = Cytoscape.getCyNode(new Integer(v.get_id()).toString());
                    CyEdge edge = Cytoscape.getCyEdge(prevNode, currNode, "interaction", "pp", false, false);
                    edges.add(edge);
                }
                prevNode = Cytoscape.getCyNode(new Integer(v.get_id()).toString());
            }
            Cytoscape.getCurrentNetwork().setSelectedEdgeState(edges, true);
        }
    }
}
